//
//  UUIntentOrderDetailActionVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 04/03/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUIntentOrderDetailActionVC.h"
#import "UUResourceManager.h"
#import "UUIntentOrderManager.h"
#import "UUPermissionManager.h"

@interface UUIntentOrderDetailActionVC ()

@property (nonatomic, strong) UIButton *ignoreButton;
@property (nonatomic, strong) UIButton *bidButton;

@end

@implementation UUIntentOrderDetailActionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    DKAssert(self.orderModel != nil);
    
    self.ignoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.ignoreButton setTitle:@"忽略" forState:UIControlStateNormal];
    self.ignoreButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.ignoreButton setTitleColor:COMMON_TEXT_ORANGE_COLOR forState:UIControlStateNormal];
    [self.ignoreButton setBackgroundImage:GetResourceManager().buttonImage3 forState:UIControlStateNormal];
    [self.ignoreButton setBackgroundImage:GetResourceManager().buttonImage3HL forState:UIControlStateHighlighted];
    [self.ignoreButton addTarget:self action:@selector(ignoreOrder) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.ignoreButton];
    [self.ignoreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(45);
        make.centerY.mas_equalTo(0).offset(-20);
    }];
    
    self.bidButton = [UIButton buttonWithType:UIButtonTypeCustom];
    if (self.orderModel.isBid) {
        [self.bidButton setTitle:@"重新报价" forState:UIControlStateNormal];
    } else {
        [self.bidButton setTitle:@"报价" forState:UIControlStateNormal];
    }
    self.bidButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.bidButton setBackgroundImage:GetResourceManager().buttonImage1 forState:UIControlStateNormal];
    [self.bidButton setBackgroundImage:GetResourceManager().buttonImage1HL forState:UIControlStateHighlighted];
    [self.bidButton addTarget:self action:@selector(bidOrder) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.bidButton];
    [self.bidButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.ignoreButton.mas_right).offset(20);
        make.right.mas_equalTo(-15);
        make.centerY.height.equalTo(self.ignoreButton);
        make.width.equalTo(self.ignoreButton).multipliedBy(1.3);
    }];
    
    if (![GetPermissionManager() checkPermission:UUPermissionFlagOrderReadWrite]) {
        self.ignoreButton.enabled = NO;
        self.bidButton.enabled = NO;
    }
    
    self.preferredContentSize = CGSizeMake(0, 20 + 45 + 40);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)ignoreOrder {
    [UIAlertController presentAlertControllerWithTitle:@"确定忽略该报价？" message:nil doneBlock:^{
        [self updateIntentOrderWithActionType:UUIntentOrderActionTypeIgnore price:nil];
    } cancelBlock:nil];
}

- (void)bidOrder {
    WEAK_SELF();
    [UIAlertController presentAlertControllerWithTitle:@"请输入报价"
                                               message:nil
                                        textFieldMaker:^(UITextField *textField) {
                                            textField.keyboardType = UIKeyboardTypeDecimalPad;
                                        } doneBlock:^(NSString *result) {
                                            if ([result isNumber]) {
                                                [weakSelf updateIntentOrderWithActionType:UUIntentOrderActionTypeBid
                                                                                    price:@([result doubleValue] * 100)];
                                            } else {
                                                [UIAlertController presentAlertControllerWithTitle:@"请输入正确的报价" message:nil doneBlock:^{
                                                    [weakSelf bidOrder];
                                                }];
                                            }
                                        } cancelBlock:nil];
}

- (void)updateIntentOrderWithActionType:(UUIntentOrderActionType)actionType price:(NSNumber *)price {
    ShowIndicatorInKeyWindow();
    WEAK_SELF();
    [GetIntentOrderManager() asyncUpdateIntentOrderWithOrderId:self.orderModel.id
                                                         price:price
                                                    actionType:actionType
                                                 completeBlock:^(NSError *error) {
                                                     HideIndicatorInKeyWindow();
                                                     
                                                     if (error == nil) {
                                                         [weakSelf.navigationController popViewControllerAnimated:YES];
                                                     } else {
                                                         ShowTipsInKeyWindow(error.localizedDescription);
                                                     }
                                                 }];
}

@end
